#ifndef WEBVIEWFORM_H
#define WEBVIEWFORM_H

#include <QWidget>
#include <QTimer>
#include <QWebPage>
#include <QWebElement>
#include "netacman.h"

namespace Ui {
    class WebViewForm;
}

class TunedPage : public QWebPage
{
    Q_OBJECT

protected:
    virtual QString userAgentForUrl(const QUrl& url) const;

public:
    TunedPage(QObject* parent = 0) : QWebPage (parent) {}
};

class WebViewForm : public QWidget
{
    Q_OBJECT
public:
    WebViewForm(QWidget *parent = 0);
    ~WebViewForm();

    typedef enum {PAGE_NONE,
                  PAGE_LOGIN,
                  PAGE_LOGOUT,
                  PAGE_INDEX,
                  PAGE_DOZOR,
                  PAGE_DOZORMONSTER,
                  PAGE_MINE,
                  PAGE_MINEOPEN,
                  PAGE_MINESHOP,
                  PAGE_HARBOUR,
                  PAGE_HARBOURPIER,
                  PAGE_CLAN,
                  PAGE_CLANTREASURY,
                  PAGE_VILLAGE,
                  PAGE_SMITH,
                  PAGE_SMITHMASTER,
                  PAGE_SMITHMASTERUPGRADE,
                  PAGE_POST,
                  PAGE_STAFF,
                  PAGE_FIGHTLOG,
                  PAGE_CASTLE,
                  PAGE_CASTLEZOO
    } PageType;
    typedef enum {FISH_NONE,
                  FISH_READY,
                  FISH_WAIT,
                  FISH_DO,
                  FISH_END
    } FishState;
    typedef enum {DOZOR_NONE,
                  DOZOR_READY,
                  DOZOR_DO,
                  DOZOR_END,
                  DOZOR_MONSTER,
                  DOSOR_FIGHT
    } DozorState;
    typedef enum {NOTIFY_NONE,
                  NOTIFY_LIST,
                  NOTIFY_TRAY,
                  NOTIFY_MESSAGE,
                  NOTIFY_FILE,
                  NOTIFY_XMMP
    } NotifyType;

protected:
    void changeEvent(QEvent *e);

private:
    Ui::WebViewForm *ui;
    NetworkAccessManager *networkAccessManager;
    PageType  currentPage;
    FishState fishFlag;
    TunedPage *page;
    QWebFrame *mainFrame;
    QTimer *mineTimer;
    QTimer *dozorMonsterTimer;
    QTimer *harbourPierWaitTimer;
    QTimer *autoactionTimer;
    QWebElement input;
    QString newLocation;
    QString oldLocation;
    int interval;

    void initWidget();
    void initState();
    void createConnections();
    QString gameServer;
    QString userLogin;
    QString userPassword;
    int minPercent;
    float digInterval;
    float workInterval;
    int boats;
    int smallGlade;
    int bigGlade;
    int fishJihad;
    unsigned short pickaxe;
    unsigned short goggles;
    unsigned short helmet;
    bool buyPickaxe;
    bool buyGoggles;
    bool buyHelmet;
    unsigned short minePickaxe;
    unsigned short mineGoggles;
    unsigned short mineHelmet;
    unsigned short level;
    bool buyMinePickaxe;
    bool buyMineGoggles;
    bool buyMineHelmet;
    bool miner;
    bool use_Dozor;
    bool use_Scares;
    bool use_MinerShopping;
    bool use_MineWay;
    DozorState dozorState;
    NotifyType notifyType;
    int gold;
    int crystal;
    int piranhas;

    PageType whatIsPage();
    void readSettings();
    void test();

    void cancelAutoaction(bool ok=false);
    void GoTo(const QString& url = QString(), bool rightNow = false);
    void GoToWork(const QString& defaultUrl="");
    void clickButton(bool rightNow = false);

    void setupNextDayWatchdog();
private slots:
    void newDay();

    void loadStarted();
    void loadFinished(bool ok);

    void linkClick();
    void buttonClick();
    void getInfo();

    void login();
    void logout();
    void index();
    void mine();
    void checkDozor();
    void dozor();
    void dozorMonster();
    void fightLog();
    void mineOpen();
    void mineShop();
    void checkPier();
    void harbourPier();
    void farm();
    void shop();
    void house();
    void training();
    void village();
    void witch();
    void post();
    void staff();
    void smith();
    void checkCastle();
    void castle();

signals:
    void log(const int &, const QString &);
    void evalStateMashine(PageType);
};

#endif // WEBVIEWFORM_H
